/**
 * AfterSaleAction.java
 * Created at 2018-1-9
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.app.aftersale.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broadtext.app.aftersale.service.AfterSaleService;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.DateUtils;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.system.service.ISysUserService;
import com.broadtext.rms.system.service.IWorkOrderService;

/**
 * <p>ClassName: AfterSaleAction</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-1-9</p>
 */
@Controller
@RequestMapping("/app/aftersale")
public class AfterSaleAction {
    
    @Autowired
    @Qualifier("afterSaleService")
    private AfterSaleService afterSaleService;
    
    /**
     * 用户Service
     */
    @Autowired
    @Qualifier("sysUserService")
    private ISysUserService sysUserService;
    
    @Autowired
    @Qualifier("workOrderService")
    private IWorkOrderService workOrderService;
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getClientID.mvc")
    @ResponseBody
    public void getClientID(HttpServletRequest request, HttpServletResponse response) {
        String clientID = request.getParameter("cid");
        System.out.println(clientID+"++++++++++++++++++++++++++cid");
    }

    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getServiceRequestDataList.mvc")
    @ResponseBody
    public List<Object> getServiceRequestDataList(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        //登录名
        String loginName = request.getParameter("loginName");
        //是否查询全部
        String isQueryAll = request.getParameter("isQueryAll");
        if(StringUtils.isNotBlank(isQueryAll)&&"0".equals(isQueryAll)){
            String submitter = this.afterSaleService.getUserIdByLoginName(loginName);
            params.put("submitter", submitter);
        }
        //请求状态
        String requestStatus = request.getParameter("requestStatus");
        if(StringUtils.isNotBlank(requestStatus)){
            params.put("requestStatus", requestStatus);
        }
        //根据登录名获取客户id
        String cusId = this.sysUserService.getCustomerId(loginName);
        params.put("customer", cusId);
        list = this.afterSaleService.getServiceRequestDataList(params);
        return list;
    }
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getWorkOrderDataList.mvc")
    @ResponseBody
    public List<Object> getWorkOrderDataList(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        /*//登录名
        String loginName = request.getParameter("loginName");
        //是否查询全部
        String isQueryAll = request.getParameter("isQueryAll");
        if(StringUtils.isNotBlank(isQueryAll)&&"0".equals(isQueryAll)){
            String submitter = this.afterSaleService.getUserIdByLoginName(loginName);
            params.put("submitter", submitter);
        }
        //请求状态
        String requestStatus = request.getParameter("requestStatus");
        if(StringUtils.isNotBlank(requestStatus)){
            params.put("requestStatus", requestStatus);
        }
        //根据登录名获取客户id
        String cusId = this.sysUserService.getCustomerId(loginName);
        params.put("customer", cusId);*/
        //请求状态
        String woType = request.getParameter("woType");
        if(StringUtils.isNotBlank(woType)){
            params.put("woType", woType);
        }
        list = this.afterSaleService.getWorkOrderDataList(params);
        return list;
    }
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getWorkOrderHistoryList.mvc")
    @ResponseBody
    public List<Object> getWorkOrderHistoryList(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        String woId = request.getParameter("woId");
        if(StringUtils.isNotBlank(woId)){
            params.put("woId", woId);
        } else {
            return null;
        }
        list = this.afterSaleService.getWorkOrderHistoryList(params);
        return list;
    }
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getWorkOrderListGroupByStatus.mvc")
    @ResponseBody
    public Map<String, Object> getWorkOrderListGroupByStatus(HttpServletRequest request, HttpServletResponse response) {
    	SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date begin = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(begin));
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        /*//登录名
        String loginName = request.getParameter("loginName");
        //是否查询全部
        String isQueryAll = request.getParameter("isQueryAll");
        if(StringUtils.isNotBlank(isQueryAll)&&"0".equals(isQueryAll)){
            String submitter = this.afterSaleService.getUserIdByLoginName(loginName);
            params.put("submitter", submitter);
        }
        //请求状态
        String requestStatus = request.getParameter("requestStatus");
        if(StringUtils.isNotBlank(requestStatus)){
            params.put("requestStatus", requestStatus);
        }
        //根据登录名获取客户id
        String cusId = this.sysUserService.getCustomerId(loginName);
        params.put("customer", cusId);*/
        //
        //登录名
        String loginName = request.getParameter("loginName");
        //查询范围
        String customerRang = request.getParameter("customerRang");
        if(StringUtils.isNotBlank(loginName)) {
            try {
                Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                    //该用户是客户账号
                    if(StringUtils.isNotBlank(customerRang)&&"1".equals(customerRang)){
                        //查公司全部
                        params.put("cusId", checkMap.get("cusId"));
                    }else {
                        //查个人提交
                        params.put("submitter", this.afterSaleService.getUserIdByLoginName(loginName));
                    }
                }
            } catch (DaoException e) {
                e.printStackTrace();
            }
            
        }
        //查询全部，还是我参与的 0 全部 1 我参与的
        String rangType = request.getParameter("rangType");
        if(StringUtils.isNotBlank(rangType)&&StringUtils.isNotBlank(loginName)&&"1".equals(rangType)){
            params.put("loginName", loginName);
        }
        //请求状态
        String woType = request.getParameter("woType");
        if(StringUtils.isNotBlank(woType)){
            params.put("woType", woType);
        }
        //请求状态
        String cusId = request.getParameter("cusId");
        if(StringUtils.isNotBlank(cusId)){
            params.put("cusId", cusId);
        }
        //请求状态
        String woContent = request.getParameter("woContent");
        if(StringUtils.isNotBlank(woContent)){
            params.put("woContent", woContent);
        }
        //未处理
        params.put("woStatus", "1");
        List<Object> notProcessedList = new ArrayList<Object>();
        notProcessedList = this.afterSaleService.getWorkOrderDataList(params);
        /*Date now = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(now));*/
        data.put("notProcessedList", notProcessedList);
        //已确认
        params.put("woStatus", "2");
        List<Object> confirmedList = new ArrayList<Object>();
        confirmedList = this.afterSaleService.getWorkOrderDataList(params);
        /*Date now1 = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(now1));*/
        data.put("confirmedList", confirmedList);
        //处理中
        params.put("woStatus", "3");
        List<Object> processingList = new ArrayList<Object>();
        processingList = this.afterSaleService.getWorkOrderDataList(params);
        /*Date now2 = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(now2));*/
        data.put("processingList", processingList);
        //已解决
        params.put("woStatus", "4");
        List<Object> solvedList = new ArrayList<Object>();
        solvedList = this.afterSaleService.getWorkOrderDataList(params);
        /*Date now3 = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(now3));*/
        data.put("solvedList", solvedList);
        //已关闭
        params.put("woStatus", "5");
        List<Object> closedList = new ArrayList<Object>();
        closedList = this.afterSaleService.getWorkOrderDataList(params);
        /*Date now4 = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求开始时间:"+f.format(now4));*/
        data.put("closedList", closedList);
        //全部
        params.remove("woStatus");
        List<Object> allList = new ArrayList<Object>();
        allList = this.afterSaleService.getWorkOrderDataList(params);
        data.put("allList", allList);
        Date end = new Date();
    	System.out.println(">>>>>>>>>>>>>>>请求结束时间:"+f.format(end));
        return data;
        
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param woId
     * @param woStatus
     * @param woType
     * @return
     */
    @RequestMapping(value = "/updateWoStatus.mvc")
    @ResponseBody
    public Map<String, Object> updateWoStatus(HttpServletRequest request ,String woId, String woStatus, String woType) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //登录名
        String loginName = request.getParameter("loginName");
        //String updateUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try{
            this.workOrderService.updateWoStatus(woId, woStatus, woType, loginName);
            dataMap.put("msg", "SUCCESS");
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getAfterSaleIndexData.mvc")
    @ResponseBody
    public Map<String, Object> getAfterSaleIndexData(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        Date now = new Date();
        String beginTime = DateUtils.getDayStartStr(now);
        params.put("beginTime", beginTime);
        params.put("woType", "1");
        //登录名
        String loginName = request.getParameter("loginName");
        //查询范围
        String customerRang = request.getParameter("customerRang");
        //今日新增
        List<Object> appendToday = new ArrayList<Object>();
        if(StringUtils.isNotBlank(loginName)) {
            try {
                Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                    //该用户是客户账号
                    beginTime = DateUtils.getPastHoursAgoTime(now, 48);
                    params.put("beginTime", beginTime);
                    params.put("createUser", loginName);
                    if(StringUtils.isNotBlank(customerRang)&&"1".equals(customerRang)){
                        //查公司全部
                        params.put("cusId", checkMap.get("cusId"));
                    }else {
                        //查个人提交
                        params.put("submitter", this.afterSaleService.getUserIdByLoginName(loginName));
                    }
                    
                }
            } catch (DaoException e) {
                e.printStackTrace();
            }
            
        }
        appendToday = this.afterSaleService.getWorkOrderDataList(params);
        data.put("appendToday", appendToday);
        Map<String,Object> requestCount = this.afterSaleService.getRequestCount(params);
        data.put("requestCount", requestCount);
        Long count = new Long(0);
        count = this.afterSaleService.getServicePlanCount(params);
        data.put("planCount", count);
        return data;
    }
    
    
    /**
     * 
     * <p>Description: 获取告警查询条件</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getServiceRequestCount.mvc")
    @ResponseBody
    public Map<String, Object> getServiceRequestCount(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        String beginTime = request.getParameter("beginTime");
        if(StringUtils.isNotBlank(beginTime)&&!"null".equals(beginTime)){
           params.put("beginTime", beginTime);
           //登录名
           String loginName = request.getParameter("loginName");
           if(StringUtils.isNotBlank(loginName)) {
               try {
                   Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                   if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                       params.put("createUser", loginName);
                   }
               } catch (DaoException e) {
                   e.printStackTrace();
               }
               
           }
           Long count = this.afterSaleService.getServiceRequestCount(params);
           data.put("count", count);   
        }else {
            data.put("count", 0);
        }
        return data;
    }
    
    
    
}
